package com.yj.ccs.abs.web.common.controller;

import com.yj.ccs.abs.boot.authorization.annotation.CurrentUser;
import com.yj.ccs.abs.common.model.BaseResult;
import com.yj.ccs.abs.common.service.FileService;
import com.yj.ccs.abs.web.common.entity.Attachment;
import com.yj.ccs.abs.web.management.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/16
 * Author     : Unbelievable9
 **/
@Api(tags = "Web-Common-Controller")
@RestController
@RequestMapping("/web/common")
public class CommonController {

    private final FileService fileService;

    @Autowired
    public CommonController(FileService fileService) {
        this.fileService = fileService;
    }

    @ApiOperation("上传文件")
    @PostMapping(value = "/upload-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<BaseResult> uploadFile(@ApiParam(value = "文件类型<br>1-账务公司-营业执照<br>2-企业客户-股权关系<br>3-企业客户-营业执照<br>4-发票信息-发票<br>5-结算信息-结算单据<br>6-回款信息-回款单据<br>7-其他信息-备注附件<br>8-基础信息-保险单<br>9-基础信息-担保文件<br>10-基础信息-质保文件<br>11-基础信息-展期条款文件<br>12-合同信息-合同影印件<br>13-合同信息-委托运输协议<br>14-合同信息-委托收货协议<br>15-合同信息-委托结算协议<br>16-货权信息-货物照片<br>17-货权信息-运输工具照片<br>18-货权信息-运输照片<br>19-货权信息-过衡单<br>20-货权信息-化验单<br>21-货权信息-货权转移证明", required = true) @RequestParam Integer file_type,
                                                 @ApiParam(value = "文件", required = true) @RequestParam MultipartFile file,
                                                 @CurrentUser User user) throws IOException {
        if (fileService.checkFileSize(file)) {
            Attachment attachment = fileService.uploadFile(file_type, file);

            return new ResponseEntity<>(BaseResult.success("上传营业执照成功").setData(attachment), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(BaseResult.error("文件大小超过限制, 无法上传"), HttpStatus.OK);
        }
    }
}
